<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      body {
        height: 3000px;
      }
    </style>
    <script src="../vue.js/vue.js"></script>
  </head>

  <body>
    <div id="baseExample">
      <h3>Scroll down inside this section ↓</h3>
      <p v-pin:[direction]="400">{{message}}</p>
      <button @click="direction = direction == 'left' ? 'top': 'left'">
        Toggle pinned position
      </button>
    </div>

    <script>
      Vue.directive("pin", (el, binding, oldValue) => {
        el.style.position = "fixed";
        var arr = ["left", "top"];
        var s = arr.filter((item) => binding.arg === item);

        arr.forEach((item) => (el.style[item] = "0px"));
        el.style[s] = binding.value + "px";
      });
      const vm = new Vue({
        el: "#baseExample",
        data: function () {
          return {
            message: "I am pinned onto the page at 400px to the left.",
            direction: "left",
          };
        },
      });
    </script>
  </body>
</html>
